package com.software.entity;

import com.alibaba.fastjson.JSON;
import com.software.utils.JDBCUtil;
import jdk.nashorn.internal.scripts.JD;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.UUID;

/**
 * 传感器
 */
public class Sensor {


    /**
     * 设备编号
     */
    private String id;

    /**
     * 监测区域
     */
    private String location;

    /**
     * 监测阈值
     */
    private String threshold;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 监测的气体类型
     */
    private String monitorType;

    /**
     * 设备类型
     */
    private String modelType;

    /**
     * 设备名称
     */
    private String modelName;

    /**
     * 设备链接
     */
    private String modelUrl;

    /**
     * 设备位置
     */
    private String modelPosition;

    /**
     * 设备角度
     */
    private String modelAngle;

    /**
     * 无参构造方法
     */
    public Sensor() {
    }

    /**
     * 有参构造方法
     */
    public Sensor(String id, String location, String threshold, Date createTime, String modelType, String modelName, String modelUrl, String modelPosition, String modelAngle) {
        this.id = id;
        this.location = location;
        this.threshold = threshold;
        this.createTime = createTime;
        this.modelType = modelType;
        this.modelName = modelName;
        this.modelUrl = modelUrl;
        this.modelPosition = modelPosition;
        this.modelAngle = modelAngle;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getThreshold() {
        return threshold;
    }

    public void setThreshold(String threshold) {
        this.threshold = threshold;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getMonitorType() {
        return monitorType;
    }

    public void setMonitorType(String monitorType) {
        this.monitorType = monitorType;
    }

    public String getModelType() {
        return modelType;
    }

    public void setModelType(String modelType) {
        this.modelType = modelType;
    }

    public String getModelName() {
        return modelName;
    }

    public void setModelName(String modelName) {
        this.modelName = modelName;
    }

    public String getModelUrl() {
        return modelUrl;
    }

    public void setModelUrl(String modelUrl) {
        this.modelUrl = modelUrl;
    }

    public String getModelPosition() {
        return modelPosition;
    }

    public void setModelPosition(String modelPosition) {
        this.modelPosition = modelPosition;
    }

    public String getModelAngle() {
        return modelAngle;
    }

    public void setModelAngle(String modelAngle) {
        this.modelAngle = modelAngle;
    }

    @Override
    public String toString() {
        return "Sensor{" +
                "id='" + id + '\'' +
                ", location='" + location + '\'' +
                ", threshold='" + threshold + '\'' +
                ", createTime=" + createTime +
                ", monitorType='" + monitorType + '\'' +
                ", modelType='" + modelType + '\'' +
                ", modelName='" + modelName + '\'' +
                ", modelUrl='" + modelUrl + '\'' +
                ", modelPosition='" + modelPosition + '\'' +
                ", modelAngle='" + modelAngle + '\'' +
                '}';
    }

    /**
     * 查询所有的传感器
     */

    public String selectAll() {
        String result = "";
        try {
            ResultSet resultSet = JDBCUtil.getAllResult("equipment", null);
            resultSet.last();
            // 3、获取数据的行数
            int row = resultSet.getRow();
            // 4、创建二氧化碳对象数组
            Sensor[] sensors = new Sensor[row];
            // 5、调用beforeFirst方法重置指针位置
            resultSet.beforeFirst();
            // 6、向数组中添加数据
            int i = 0;
            while (i < row) {
                resultSet.next();
                sensors[i] = new Sensor(
                        resultSet.getString(1),
                        resultSet.getString(2),
                        resultSet.getString(3),
                        resultSet.getDate(4),
                        resultSet.getString(5),
                        resultSet.getString(6),
                        resultSet.getString(7),
                        resultSet.getString(8),
                        resultSet.getString(9)
                );
                i++;
            }
            // 7、释放数据库连接资源
            JDBCUtil.closeConnection(resultSet);
            // 8、返回JSON格式的数据集
            result = JSON.toJSONString(sensors);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 添加传感器
     */
    public String add(String modelPosition) {
        String result = "";
        Connection connection = JDBCUtil.getConnection();
        Sensor sensor = new Sensor();
        sensor.setId(UUID.randomUUID().toString().replace("-", ""));
        sensor.setThreshold("0.03%");
        sensor.setLocation("厂房配电间");
        sensor.setModelName("thing88");
        sensor.setModelUrl("/api/models/303d3414e368462197cc6f50ed2f1917/0/gltf/");
        sensor.setModelType("Thing");
        sensor.setModelAngle("[0.0, 0.0, 0.0]");
        sensor.setModelPosition(modelPosition);
        try {
            PreparedStatement statement = connection.prepareStatement("INSERT INTO equipment (id,location,threshold, model_type,model_name,model_url, model_position,model_angle) VALUES (?,?,?,?,?,?,?,?)");
            statement.setString(1, sensor.getId());
            statement.setString(2, sensor.getLocation());
            statement.setString(3, sensor.getThreshold());
            statement.setString(4, sensor.getModelType());
            statement.setString(5, sensor.getModelName());
            statement.setString(6, sensor.getModelUrl());
            statement.setString(7, sensor.getModelPosition());
            statement.setString(8, sensor.getModelAngle());
            int insert = statement.executeUpdate();
            if (insert > 0) {
                result = "success";
            } else {
                result = "fail";
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 查询一台传感器
     * @param modelName
     * @return
     */
    public Sensor selectOne(String modelName) {
        Sensor sensor = null;
        ResultSet resultSet = null;
        Connection connection = JDBCUtil.getConnection();
        PreparedStatement statement = null;
        try {
            statement = connection.prepareStatement("select * from equipment where model_name=?");
            statement.setString(1, modelName);
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                sensor = new Sensor();
                sensor.setId(resultSet.getString(1));
                sensor.setLocation(resultSet.getString(2));
                sensor.setThreshold(resultSet.getString(3));
                sensor.setCreateTime(resultSet.getDate(4));
                sensor.setModelType(resultSet.getString(5));
                sensor.setModelName(resultSet.getString(6));
                sensor.setModelUrl(resultSet.getString(7));
                sensor.setModelPosition(resultSet.getString(8));
                sensor.setModelAngle(resultSet.getString(9));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (resultSet != null) {
                resultSet.close();
            }
            if (statement != null) {
                connection.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return sensor;
    }

    /**
     * 更新传感器
     */
    public String update(String id, String location, String threshold) {
        String update = "";
        Connection connection = JDBCUtil.getConnection();
        PreparedStatement statement = null;
        try {
            statement = connection.prepareStatement("update equipment set location=?,threshold=? where id=?");
            statement.setString(1, location);
            statement.setString(2, threshold);
            statement.setString(3, id);
            int i = statement.executeUpdate();
            if (i > 0) {
                update = "success";
            } else {
                update = "fail";
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        try {
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return update;

    }

}
